//
//  TstGroupChatVC.swift
//  TstChat
//
//  Created by wanghui on 2018/5/10.
//  Copyright © 2018年 daerwei. All rights reserved.
//

import UIKit
import Kingfisher
//import Addtions

class TstGroupChatVC: ChatBaseController {
    lazy var tableView:UITableView = {
        let tableView = UITableView(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height), style: .plain)
        
        tableView.separatorStyle = .singleLine
        tableView.tableFooterView = UIView.init(frame: CGRect.zero)
        tableView.dataSource = (self as UITableViewDataSource)
        tableView.delegate = (self as UITableViewDelegate)
        
        tableView.register(TstGroupchatCell.self, forCellReuseIdentifier: "TstGroupchatCell")
        tableView.register(TstGroupchatcellFortwo.self, forCellReuseIdentifier: "TstGroupchatcellFortwo")
        tableView.register(UINib(nibName: "TstGroupchatcellFortwo",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellFortwo")
        tableView.register(UINib(nibName: "TstGroupchatcellForthree",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForthree")
        tableView.register(UINib(nibName: "TstGroupchatcellForFour",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForFour")
        tableView.register(UINib(nibName: "TstGroupchatcellForFive",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForFive")
        tableView.register(UINib(nibName: "TstGroupchatcellForsix",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForsix")
        tableView.register(UINib(nibName: "TstGroupchatcellForseven",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForseven")
        tableView.register(UINib(nibName: "TstGroupchatcellForeight",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellForeight")
        tableView.register(UINib(nibName: "TstGroupchatcellFornine",bundle: nil), forCellReuseIdentifier: "TstGroupchatcellFornine")
        
        tableView.rowHeight = 70
        tableView.backgroundColor = RGBA(r: 241/255, g: 240/255, b: 245/255, a: 1)
        return tableView
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isTranslucent = true
        loadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        notificationObserve()
    }
    
    let service = SessionService()
    var sessionList = [ChatSession]()
    
    func  loadData() {
        DispatchQueue.global().async {
            self.sessionList = self.service.getRecentSessions(chatType: .group)
            if self.sessionList.count > 0 {
                var unreadcount:Int = 0
                for session in self.sessionList{
                    if !session.noDisturbing {
                        unreadcount += session.unreadCount
                    }
                }
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                    if unreadcount > 0 {
                        self.tabBarItem.pp.addBadge(text: "\(unreadcount > 99 ? "99+" : "\(unreadcount)")")
                    }else {
                        self.tabBarItem.pp.hiddenBadge()
                    }
                }
            }
        }
    }
    
    func loadUnreadCount(){
        DispatchQueue.global().async {
            self.sessionList = self.service.getRecentSessions(chatType: .group)
            if self.sessionList.count > 0 {
                var unreadcount: Int = 0
                for session in self.sessionList {
                    if !session.noDisturbing {
                        unreadcount += session.unreadCount
                    }
                }
                DispatchQueue.main.async {
                    self.tabBarItem.pp.addBadge(number: unreadcount, max: 99, maxStr: "99+")
                }
            }
        }
    }
    
    func notificationObserve() {
        // 添加通知监听
        NotificationCenter.default.addObserver(self, selector: #selector(getnewMsg(notification:)), name: Notification.Name("onNewMsg"), object: nil)
        
    }
    
    func setUI() {
        self.view.backgroundColor = RGBA(r: 241/255, g: 240/255, b: 245/255, a: 1)
        self.view.addSubview(tableView)
        
        // Do any additional setup after loading the view.
        
        //设置导航栏的背景色
        //    self.navigationController?.navigationBar.barTintColor = UIColor.black
        self.navigationController?.navigationBar.barStyle = .black
        self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        
        //设置导航栏的左右侧按钮  首页 建群
        let btn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 40, height: 30))
        btn.setTitle("返回", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.isUserInteractionEnabled = true
        btn.addTarget(self, action: #selector(buttonTap(button:)), for: UIControl.Event.touchUpInside)
        let homepageview = UIBarButtonItem(customView: btn)
        
        let spacer = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        spacer.width = -20
        // 首页按钮设置成功
        self.navigationItem.leftBarButtonItems = [spacer, homepageview]
        
        let btntwo = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 40, height: 30))
        btntwo.setTitle("建群", for: .normal)
        btntwo.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btntwo.isUserInteractionEnabled = true
        btntwo.addTarget(self, action: #selector(rightbuttonTap(button:)), for: UIControl.Event.touchUpInside)
        let setupgroup = UIBarButtonItem(customView: btntwo)
        let spacertwo = UIBarButtonItem.init(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        spacertwo.width = -5
        self.navigationItem.rightBarButtonItems = [ setupgroup]
    }
    
    @objc func getnewMsg(notification: Notification) {
        loadData()
    }
    
    @objc func buttonTap(button:UIButton) {
        //        print("点击了首页按钮")
        self.tabBarController?.navigationController?.popViewController(animated: true)
    }
    
    @objc func rightbuttonTap(button:UIButton) {
        print("点击了建群按钮")
        let vc = TstChooseContactVC()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
        //        self.hidesBottomBarWhenPushed = false
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

extension TstGroupChatVC: UITableViewDataSource, UITableViewDelegate{
    // datasource
    internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let group = self.sessionList[indexPath.row]
        let memberCount = group.avatars.count
        switch memberCount {
        case 1:
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatCell", for: indexPath) as! TstGroupchatCell
            let group = self.sessionList[indexPath.row]
            cell.nameLabel.text = group.sessionName
            cell.nodisturbImage.isHidden = !group.noDisturbing
            cell.lastestnews.text = group.lastMsg?.msgText
            cell.latestTime.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.headimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            
            if group.unreadCount > 0 {
                if cell.nodisturbImage.isHidden == true {
                    cell.headview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.headview.pp.moveBadge(x: 0, y: 1)
                    cell.headview.pp.setBadge(flexMode: .head)
                }else {
                    cell.headview.pp.addDot(color: .red)
                    cell.headview.pp.moveBadge(x: 0, y: 1)
                }
            } else {
                cell.headview.pp.hiddenBadge()
            }
            return cell
        case 2 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellFortwo", for: indexPath) as! TstGroupchatcellFortwo
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            } else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        case 3 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForthree", for: indexPath) as! TstGroupchatcellForthree
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        case 4 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForFour", for: indexPath) as! TstGroupchatcellForFour
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        case 5 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForFive", for: indexPath) as! TstGroupchatcellForFive
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        case 6 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForsix", for: indexPath) as! TstGroupchatcellForsix
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        case 7 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForseven", for: indexPath) as! TstGroupchatcellForseven
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.sevenimg.kf.setImage(with: Common.avatarUrl(group.avatars[6] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        case 8 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellForeight", for: indexPath) as! TstGroupchatcellForeight
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.sevenimg.kf.setImage(with: Common.avatarUrl(group.avatars[6] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.eighthimg.kf.setImage(with: Common.avatarUrl(group.avatars[7] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        case 9 :
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatcellFornine", for: indexPath) as! TstGroupchatcellFornine
            let group = self.sessionList[indexPath.row]
            cell.namelabel.text = group.sessionName
            cell.nodisturbingImgview.isHidden = !group.noDisturbing
            cell.lattestnewslabel.text = group.lastMsg?.msgText
            cell.lattesttimelabel.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.firstimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.secondimg.kf.setImage(with: Common.avatarUrl(group.avatars[1] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.thirdimg.kf.setImage(with: Common.avatarUrl(group.avatars[2] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fourthimg.kf.setImage(with: Common.avatarUrl(group.avatars[3] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.fifthimg.kf.setImage(with: Common.avatarUrl(group.avatars[4] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.sixthimg.kf.setImage(with: Common.avatarUrl(group.avatars[5] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.sevenimg.kf.setImage(with: Common.avatarUrl(group.avatars[6] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.eighthimg.kf.setImage(with: Common.avatarUrl(group.avatars[7] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            cell.nineimg.kf.setImage(with: Common.avatarUrl(group.avatars[8] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            if group.unreadCount > 0 {
                if cell.nodisturbingImgview.isHidden == true {
                    cell.clearview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                    cell.clearview.pp.setBadge(flexMode: .head)
                    
                }else {
                    cell.clearview.pp.addDot(color: .red)
                    cell.clearview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.clearview.pp.hiddenBadge()
            }
            return cell
        default:
            let cell = tableView.dequeueReusableCell(withIdentifier: "TstGroupchatCell", for: indexPath) as! TstGroupchatCell
            let group = self.sessionList[indexPath.row]
            cell.nameLabel.text = group.sessionName
            cell.nodisturbImage.isHidden = !group.noDisturbing
            cell.lastestnews.text = group.lastMsg?.msgText
            cell.latestTime.text = TimeUtil.instance.chatTimeStr(group.lastMsg?.createTime.timeIntervalSince1970 ?? 0)
            cell.headimage.kf.setImage(with: Common.avatarUrl(group.avatars[0] ) ?? URL(string: "localhost")!, placeholder: #imageLiteral(resourceName: "defaultAvatar"),options: [.processor(CryptorProcessor())])
            
            if group.unreadCount > 0 {
                if cell.nodisturbImage.isHidden == true {
                    cell.headview.pp.addBadge(text: "\(group.unreadCount > 99 ? "99+" : "\(group.unreadCount)")")
                    cell.headview.pp.moveBadge(x: 0, y: 1)
                    cell.headview.pp.setBadge(flexMode: .head)
                }else {
                    cell.headview.pp.addDot(color: .red)
                    cell.headview.pp.moveBadge(x: 0, y: 1)
                }
            }else {
                cell.headview.pp.hiddenBadge()
            }
            return cell
        }
    }
    
    // delegate
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.sessionList.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let group = self.sessionList[indexPath.row]
        //      let chatvc = ChatViewController()
        //      chatvc.chatType = group.session.chatType.rawValue as NSNumber
        //      chatvc.sessionServerId = group.session.sessionServerId as NSNumber
        //      chatvc.sessionName = group.sessionName
        //      chatvc.hidesBottomBarWhenPushed = true
        //      self.navigationController?.pushViewController(chatvc, animated: true)
        //      self.hidesBottomBarWhenPushed = false
    }
    
    
}


